package com.hzsk.lisence.controller;

import com.alibaba.fastjson.JSONObject;
import com.hzsk.lisence.entity.SoftwareLicenseEntity;
import com.hzsk.lisence.entity.query.LisenceServiceQuery;
import com.hzsk.lisence.entity.response.IncResponce;
import com.hzsk.lisence.mapper.SoftwareLicenseMapper;
import com.hzsk.lisence.service.SoftwareLicenseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@CrossOrigin
@RestController
@RequestMapping("/api/lisence/software/license")
public class SoftwareLicenseController {


     @Resource
    private SoftwareLicenseService softwareLicenseService;

    @Resource
    private SoftwareLicenseMapper softwareLicenseMapper;


    @PostMapping("saveshsoftwarelicense")
    @ApiOperation(value = "保存授权许可信息")
    public IncResponce saveSoftwareLicense(@RequestBody SoftwareLicenseEntity softwareLicenseEntity){
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(softwareLicenseService.saveSoftwareLicense(softwareLicenseEntity,softwareLicenseEntity.getOperatorUserId()));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }

    @GetMapping("getsoftwarelicensebyid")
    @ApiOperation(value = "查看许可明细")
    public IncResponce getsoftwarelicenseById(@RequestParam("id") Integer id){
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(softwareLicenseService.getsoftwarelicenseById(id));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }


    @PostMapping("getsoftwarelicensebyuserid")
    @ApiOperation(value = "根据用户id查询许可列表")
    public IncResponce getSoftwareLicenseByUserId(@RequestBody LisenceServiceQuery lisenceServiceQuery){
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(softwareLicenseService.getSoftwareLicenseByUserId(lisenceServiceQuery));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }



    @GetMapping("publishsoftwarelicense")
    @ApiOperation(value = "发布授权许可信息")
    public IncResponce publishSoftwareLicense(@RequestParam("id") Integer id, @RequestParam("operatorUserId") Integer operatorUserId, HttpServletResponse response){
        IncResponce incResponce = new IncResponce();
        try {
            InputStream zipFileContent = softwareLicenseService.publishSoftwareLicense(id, operatorUserId);
            String contentType = "application/octet-stream"; // 根据文件类型设置相应的内容类型
            String name = softwareLicenseMapper.selectZipNameById(id) + ".zip";
            String file_name = new String(name.getBytes(), StandardCharsets.ISO_8859_1);
            response.setContentType(contentType);
            response.setContentLength(zipFileContent.available());
            // 设置响应的头部信息
            response.setHeader("Content-Disposition", "attachment; filename=" + file_name);
            FileCopyUtils.copy(zipFileContent, response.getOutputStream());
            incResponce.setData("操作成功");
            incResponce.setCode(0);
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }


    @GetMapping("publishsoftwarelicensebyjson")
    @ApiOperation(value = "JSON生成lisence")
    public IncResponce publishSoftwareLicenseByJson(@RequestParam Integer operatorUserId,@RequestBody JSONObject data){
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(softwareLicenseService.publishSoftwareLicenseByJson(operatorUserId,data));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }

    @GetMapping("confirmSoftwareLicense")
    @ApiOperation(value = "确认许可")
    public IncResponce confirmSoftwareLicense(@RequestParam Integer id,@RequestParam Integer operatorUserId){
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(softwareLicenseService.confirmSoftwareLicense(id,operatorUserId));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }

    @PutMapping("updatesoftwarelicense")
    @ApiOperation(value = "修改授权许可信息")
    public IncResponce updateSoftwareLicense(@RequestBody SoftwareLicenseEntity softwareLicenseEntity){
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(softwareLicenseService.updateSoftwareLicense(softwareLicenseEntity,softwareLicenseEntity.getOperatorUserId()));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }


    @DeleteMapping("delsoftwarelicense")
    @ApiOperation(value = "删除授权许可信息")
    public IncResponce delSoftwareLicense(@RequestParam Integer id,@RequestParam Integer operatorUserId){
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(softwareLicenseService.delSoftwareLicense(id,operatorUserId));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }





}
